<?
require_once("/var/www/html/new/com.inc.php");
class Dist{
	public $dtype=array(0=>"供应商",1=>"分销商",2=>"直接供应方",5=>"普通用户",9=>"平台帐号");
	public $status=array(0=>"正常",1=>"禁用",2=>"删除");
	private $le;

	function __construct(){
		global $le;
		$this->le=$le;
	}
	function check($info){
		foreach($info as $k=>$v){
			switch($k){
				case "dname":
					if(p_match($v) && $v){
						return "姓名存在非法字符!";
					}
					$str.="dname='".$v."',";
					break;
				case "password":
					$str.="password='".md5($v)."',";
					break;
				case "status":
					if(!in_array($v,array_keys($this->status))){return "未知的会员状态!";}
					$str.="status=".(int)$v.",";
					break;
				case "address":
					if(p_match($v) && $v){
						return "地址存在非法字符!";
					}
					$str.="address='".$v."',";
					break;
				case "mobile":
					for($i=0;$i<strlen($v);$i++){
						$t=substr($v,$i,1);
						$n=ord($t);
						if($n<48 || $n>57){
							return "联系用手机号码存在非法字符";
						}
					}
					if(strlen($v)!=11){
						return "联系用手机号码格式不对!";
					}
					if(!v){
						return "联系用手机号码为必填项!";
					}
					$str.="mobile='".$v."',";
					break;
				case "qq":
					for($i=0;$i<strlen($v);$i++){
						$t=substr($v,$i,1);
						$n=ord($t);
						if($n<48 || $n>57){
							return "QQ号存在非数字!";
						}
					}
					$str.="qq='".$v."',";
					break;
				case "headphoto":
					if(fliter_words($v)){
						return "头像保存路径存在非法字符!";
					}
					$str.="headphoto='".$v."',";
					break;
				case "cname":
					if(p_match($v) && $v){
						return "别名存在非法字符!";
					}
					$str.="cname='".$v."',";
					break;
				case "ctel":
					for($i=0;$i<strlen($v);$i++){
						$t=substr($v,$i,1);
						$n=ord($t);
						if(($n<48 || $n>57) && $n!=45){
							return "其他联系电话存在非数字!";
						}
					}
					$str.="ctel='".$v."',";
					break;
				case "endtime":
					$time=explode("-",$v);
					if(!checkdate($time[1],$time[2],$time[0])){	
						return "";
					}
					$str.="endtime='".$v."',";
					break;
				case "smstype":
					if($v!=1 && $v!=0){return "发送短信方式存在非数字!";}
					$str.="smstype=".(int)$v.",";
					break;
				case "smsauto":
					if($v!=1 && $v!=0){return "新订单短信通知存在非数字!";}
					$str.="smsauto=".(int)$v.",";
					break;
				case "dcode":
					if(p_match($v) && $v){
						return "接口编码存在非法字符!";
					}
					$str.="dcode='".$v."',";
					break;
				case "alipay":
					if($v){
						if(p_match($v)){
							return "支付宝帐号存在非法字符!";
						}
					}
					$str.="alipay='".$v."',";
					break;
				case "pay_on_order":
					if($v!=1 && $v!=0 && $v!=2){return "订单支付类型默认存在非数字!";}
					$str.="pay_on_order=".(int)$v.",";
					break;
				case "n_send":
					if($v!=1 && $v!=0 && $v!=2 && $v!=3){return "发系统通知的权限存在非数字!";}
					$str.="n_send=".(int)$v.",";
					break;
				case "n_accept":
					if($v!=1 && $v!=0 && $v!=2){return "是否接受系统通知存在非数字!";}
					$str.="n_accept=".(int)$v.",";
					break;
				case "fee_platform":
					if(filter_var($v, FILTER_VALIDATE_INT)===false){return "平台使用费存在非数字!";}
					$str.="fee_platform=".(int)$v.",";
					break;
				case "fee_bank":
					if(filter_var($v, FILTER_VALIDATE_INT)===false){return "第三方支付费用存在非数字!";}
					$str.="fee_bank=".(int)$v.",";
					break;
				case "fee_sms":
					if(filter_var($v, FILTER_VALIDATE_INT)===false){return "短信每条收费存在非数字!";}
					$str.="fee_sms=".(int)$v.",";
					break;
				case "position":
					if(p_match($v) && $v){
						return "职位存在非法字符!";
					}
					$str2.="position='".$v."',";
					break;
				case "sex":
					$v=strtoupper($v);
					if($v!="M" && $v!="F"){
						return "性别存在非法字符!";
					}
					$str2.="sex='".$v."',";
					break;
				case "email":
					if(substr_count($v,"@")!=1 && $v){
						return "电子邮件格式不对!";
					}
					$str2.="email='".$v."',";
					break;
				case "com_name":
					if(p_match($v) && $v){
						return "公司名称存在非法字符!";
					}
					$str2.="com_name='".$v."',";
					break;
				case "com_type":
					if(fliter_words($v)){
						return "公司类型存在非法字符!";
					}
					$str2.="com_type='".$v."',";
					break;
				case "province":
					$str2.="province=".(int)$v.",";
					break;
				case "city":
					$str2.="city=".(int)$v.",";
					break;
				case "country":
					$str2.="country=".(int)$v.",";
					break;
				case "fax":
					for($i=0;$i<strlen($v);$i++){
						$t=substr($v,$i,1);
						$n=ord($t);
						if(($n<48 || $n>57) && $n!=45){
							return "传真号存在非数字!";
						}
					}
					$str2.="fax='".$v."',";
					break;
				case "website":
					if(fliter_words($v)){
						return "网址存在非法字符!";
					}
					$str2.="website='".$v."',";
					break;
				case "business":
					if(p_match($v) && $v){
						return "经营范围存在非法字符!";
					}
					$str2.="business='".$v."',";
					break;
				case "short_company_name":
					if(strlen($v)>12){
						return "公司简称不可超过4个字!";
					}
					elseif(p_match($v) && $v){
						return "公司简称存在非法字符!";
					}
					$str2.="short_company_name='".$v."',";
					break;
			/*	case "tmoney":
					if(c_get($v) || $v<0){
						return "信用额度不正确!";
					}
					$str.="tmoney=".((int)$v*100).",";
					break;
				case "lvl"
					if(c_get($v)){
						return "权限等级存在非数字!";
					}
					$str.="lvl=".(int)$v.",";
					break;
				case "tplid":
					if(c_get($tplid)){
						echo "<script>alert('套用模板存在非数字!');document.getElementById('mask').style.visibility='hidden';</script>";
						exit;
					}
					$str2.="tplid=".(int)$v.",";
					break;
				case "contact"
					if(p_match($v)){
						return "联系人存在非法字符!";
					}
					$sel="select lvl from  where id=".$did;
					$this->le->query($sel);
					$this->le->fetch_assoc();
					if($this->le->f("lvl")!=2){
						return "请填写联系人!";
					}
					$str.="contact='".$v."',";
					break;*/
			}
		}
		return array(0=>$str,1=>$str2);;
	}
	function add($info=array()){
		if(!isset($info['dtype'])){return "请输入会员类型!";}
		if(!isset($info['mobile'])){return "请输入手机号!";}
		$sel="select id from pft_member where status<>2 and mobile='".$info['mobile']."' limit 1";
		$this->le->query($sel);
		$this->le->fetch_assoc();
		if($this->le->f("id")){
			return "手机号已存在";
		}
		if(!is_array($str=$this->check($info))){
			return $str;
		}
		if(!in_array($info['dtype'],array_keys($this->dtype))){return "未知的会员类型!";}
		if($info['dtype']==5){
			$account=$info['mobile'];
		}
		elseif($info['dtype']==9){
			$account="admin";
		}
		else{
			$sel="select account max from pft_member where dtype=".(int)$info['dtype']." order by account DESC limit 1";
			$this->le->query($sel);
			$this->le->fetch_assoc();
			$max=$this->le->f("max");
			if($max){
			//	$account=$max+1;
			}
			else{
				switch($info['dtype']){
					case 0:$account=100000;break;
					case 1:$account=200000;break;
					case 2:$account=600000;break;
					default:return "未知的会员类型";
				}
			}
		}
		if($account){
			$ins="insert pft_member set ".$str[0]."dtype=".(int)$info['dtype'].",account='".$account."',status=0,creattime=now()";//echo $ins;
			$this->le->query($ins);
			$id=mysql_insert_id();
		}
		else{
			$ins="insert into pft_member(account) select (account+1) from pft_member where dtype=".$info['dtype']." order by account DESC limit 1";//echo $ins;
			$this->le->query($ins);
			$id=mysql_insert_id();
			$upd="update pft_member set ".$str[0]."dtype=".(int)$info['dtype'].",creattime=now() where id=".$id." limit 1";
			$this->le->query($upd);
		}
		$ins="insert pft_member_extinfo set ".$str[1]."fid=".$id;
		$this->le->query($ins);
		return $id;
	}
	function addLow($sid,$did,$type=0){
		if(!$sid || !$did){return "缺少父或子ID";}
		$sel="select id from pft_member_relationship where parent_id=".$sid." and son_id=".$did;
		$this->le->query($sel);
		$this->le->fetch_assoc();
		if($rid=$this->le->f("id")){
			$upd="update pft_member_relationship set restoretime=now(),status=0 where id=".$rid;
			$this->le->query($upd);
		}
		else{
			$ins="insert pft_member_relationship set parent_id=".$sid.",son_id=".$did.",son_id_type=".$type.",ship_type=".$type.",status=0,rectime=now()";
			$this->le->query($ins);
		}
		if($type==0){
			$sel="select id from pft_product_sale_list where aid=".$sid." and fid=".$did;
			$this->le->query($sel);
			$this->le->fetch_assoc();
			if($rid=$this->le->f("id")){
				$upd="update pft_product_sale_list set pids='A',status=0 where id=".$rid;
				$this->le->query($upd);
			}
			else{
				$ins="insert pft_product_sale_list set pids='A',aid=".$sid.",fid=".$did;
				$this->le->query($ins);
			}
		}
		return true;
	}
	function delLow($sid,$did){
		if(!$sid || !$did){return "缺少父或子ID";}
		$upd="update pft_member_relationship set status=1,cuttime=now() where parent_id=".$sid." and son_id=".$did." limit 1";
		$this->le->query($upd);
		$upd="update pft_product_sale_list set status=1 where aid=".$sid." and fid=".$did." limit 1";
		$this->le->query($upd);
		return true;
	}
	function upd($info=array(),$did){
		if(!is_array($str=$this->check($info))){
			return $str;
		}
		if(isset($info['mobile'])){
			$sel="select id from pft_member where status<>2 and mobile='".$info['mobile']."' limit 1";
			$this->le->query($sel);
			$this->le->fetch_assoc();
			if($this->le->f("id") && $this->le->f("id")!=$did){
				return $this->le->f("id")."手机号已存在".$did;
			}
		}
		if(isset($info['alipay'])){
			$sel="select dtype from pft_member where id=".$did." limit 1";
			$this->le->query($sel);
			$this->le->fetch_assoc();
			if(($this->le->f("dtype")==0 || $this->le->f("dtype")==1) && !$info['alipay']){
				return "供应商、分销商的支付宝帐号不能为空";
			}
		}
		if(isset($info['dcode'])){
			$sel="select id from pft_member where dcode='".$info['dcode']."' limit 1";
			$this->le->query($sel);
			$this->le->fetch_assoc();
			if($this->le->f("id")!=$did){
				return "接口编码已存在!";
			}
		}
		if($str[0]){
			$upd="update pft_member set ".trim($str[0],",")." where id=".$did." limit 1";
			$this->le->query($upd);
		}
		if($str[1]){
			$upd="update pft_member_extinfo set ".trim($str[1],",")." where fid=".$did." limit 1";
			$this->le->query($upd);
		}
		return true;
	}
	function getInfo($did){
		$sel="select *,m.id id from pft_member m left join pft_member_extinfo e on fid=m.id where m.id=".$did." limit 1";
		$this->le->query($sel);
		return $this->le->fetch_assoc();
	}
}
?>